package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/member")
@CrossOrigin
public class MemberController {
    @Reference
    private MemberService memberService;

    /**
     * 查询会员数据
     * @return
     */
    @GetMapping(path = "/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        //分页查询会员信息
        try {
            PageResult pageResult = memberService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    @PostMapping(path = "/add")
    public Result add(@RequestBody Member member){
        try {
            memberService.addMemBer(member);
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }

    }

    /**
     * 编辑会员信息
     * @param member
     * @return
     */
    @PutMapping(path = "/update")
    public Result update(@RequestBody Member member){
        try {
            memberService.update(member);
            return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    /**
     * 点击编辑功能，根据id查询该会员数据
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id){
        try {
            Member member = memberService.findById(id);
            return new Result(true,MessageConstant.QUERY_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }

    }


    @PutMapping(path = "/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Integer id,@PathVariable Integer status){
        try {
            memberService.updateStatus(id,status);
            return new Result(true,MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

}
